//
//  JFAreaDataManager+ThreeLevelLinkage.m
//  huashiedu
//
//  Created by oceans on 2018/9/9.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "JFAreaDataManager+ThreeLevelLinkage.h"
#import "LocationModel.h"

@implementation JFAreaDataManager (ThreeLevelLinkage)

- (void)provinceData:(void (^)(NSMutableArray *))provinceData {
    
    NSMutableArray *resultArray = [[NSMutableArray alloc] init];
    FMResultSet *result = [self.db executeQuery:@"SELECT * FROM shop_area GROUP BY province_number;"];
    while ([result next]) {
        NSString *provinceName = [result stringForColumn:@"province_name"];
        NSString *provinceNumber = [result stringForColumn:@"province_number"];
        [resultArray addObject:@{@"province_name": provinceName, @"province_number": provinceNumber}];
    }
    provinceData(resultArray);
}

- (void)cityData:(NSString *)provinceNumber cityData:(void (^)(NSMutableArray *))cityData {
    
    NSMutableArray *resultArray = [[NSMutableArray alloc] init];
    NSString *sqlString = [NSString stringWithFormat:@"SELECT * FROM shop_area WHERE province_number ='%@' GROUP BY city_number;",provinceNumber];
    FMResultSet *result = [self.db executeQuery:sqlString];
    while ([result next]) {
        NSString *city_name = [result stringForColumn:@"city_name"];
        NSString *city_number = [result stringForColumn:@"city_number"];
        [resultArray addObject:@{@"city_name": city_name, @"city_number": city_number}];
    }
    cityData(resultArray);
}

- (void)selectAreaData:(NSString *)cityNumber areaData:(void (^)(NSMutableArray *areaData))areaData {
    NSMutableArray *resultArray = [[NSMutableArray alloc] init];
    NSString *sqlString = [NSString stringWithFormat:@"SELECT * FROM shop_area WHERE city_number ='%@';",cityNumber];
    FMResultSet *result = [self.db executeQuery:sqlString];
    while ([result next]) {
        NSString *areaName = [result stringForColumn:@"area_name"];
        NSString *areaNumber = [result stringForColumn:@"area_number"];
//        NSString *cityName = [result stringForColumn:@"city_name"];
//        NSString *cityNumber = [result stringForColumn:@"city_number"];
        [resultArray addObject:@{@"area_name": areaName, @"area_number": areaNumber}];
    }
    areaData(resultArray);
}

- (void)selectAreaData:(NSString *)cityNumber areaName:(NSString *)areaName areaData:(void (^)(NSMutableArray *))areaData {
    NSMutableArray *resultArray = [[NSMutableArray alloc] init];
    NSString *sqlString = [NSString stringWithFormat:@"SELECT * FROM shop_area WHERE city_number ='%@' AND area_name = '%@';",cityNumber, areaName];
    FMResultSet *result = [self.db executeQuery:sqlString];
    while ([result next]) {
        NSString *areaName = [result stringForColumn:@"area_name"];
        NSString *areaNumber = [result stringForColumn:@"area_number"];
        NSString *cityName = [result stringForColumn:@"city_name"];
        NSString *cityNumber = [result stringForColumn:@"city_number"];
        [resultArray addObject:@{@"area_name": areaName, @"area_number": areaNumber, @"city_name": cityName, @"city_number": cityNumber}];
    }
    areaData(resultArray);
}

- (void)selectAreaDataWithName:(NSString *)cityName areaData:(void (^)(NSMutableArray *))areaData {
    NSMutableArray *resultArray = [[NSMutableArray alloc] init];
    NSString *para = @"";
    if ([LocationModel shared].cityName.length > 0) {
        para = [NSString stringWithFormat:@" AND city_name LIKE '%@'", [LocationModel shared].cityName];
    }
    NSString *sqlString = [NSString stringWithFormat:@"SELECT * FROM shop_area WHERE area_name ='%@'%@;", cityName, para];
    FMResultSet *result = [self.db executeQuery:sqlString];
    while ([result next]) {
        NSString *areaName = [result stringForColumn:@"area_name"];
        NSString *areaNumber = [result stringForColumn:@"area_number"];
        NSString *cityName = [result stringForColumn:@"city_name"];
        NSString *cityNumber = [result stringForColumn:@"city_number"];
        [resultArray addObject:@{@"area_name": areaName, @"area_number": areaNumber, @"city_name": cityName, @"city_number": cityNumber}];
    }
    areaData(resultArray);
}

@end
